package com.service.sys;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.common.spring.SpringMvcUtils;
import com.dao.sys.SysDao;
import com.dao.sys.UserDao;
import com.model.sys.SysUser;
import com.model.sys.UserModel;

@Service("sysUserService_1")
public class SysUserService {
	static final Logger logger = LoggerFactory.getLogger(SysUserService.class);

	@Autowired
	UserDao userDao;
	@Autowired
	SysDao sysDao;

	/**
	 * 根据部门id查询本部门所有人员
	 * 
	 * @param userId
	 * @return
	 */
	public List<String> getUserIdListFromDept(String deptId) {
		List<String> userStrings = new ArrayList<String>();

		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		List<HashMap<String, Object>> filterList = new ArrayList<HashMap<String, Object>>();
		HashMap<String, Object> filterMap = new HashMap<>();
		filterMap.put("field", "deptid");
		filterMap.put("value", deptId);
		filterList.add(filterMap);
		hashMap.put("filter", filterList);

		List<UserModel> userModels = userDao.selectList(hashMap);
		for (UserModel userModel : userModels) {
			userStrings.add(userModel.getUSERID());
		}
		return userStrings;
	}

	/**
	 * 根据用户数据权限获取所有部门下属人员
	 * 
	 * @param permission
	 * @return
	 */
	public List<String> getUserIdListFromPermission(String permission) {
		List<String> userStrings = new ArrayList<String>();
		String[] permissonList = permission.split(",");
		for (String s1 : permissonList) {
			List<String> list = getUserIdListFromDept(s1);
			userStrings.addAll(list);
		}
		return userStrings;
	}

	public List<String> getUserListFromRole(String userId, String moduleid) {
		List<String> userStrings = new ArrayList<String>();
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("moduleUrl", moduleid);
		hashMap.put("userId", userId);
		List<String> permissonList = sysDao.getPermissionFromModule(hashMap);
		if (permissonList != null && permissonList.size() > 0) {
			String[] listDept = permissonList.get(0).split(",");
			for (String deptString : listDept) {
				List<String> list = getUserIdListFromDept(deptString);
				userStrings.addAll(list);
			}
		}
		return userStrings;
	}

	public List<String> getPermissionFromModuleAndUser(String userId, String moduleUrl) {
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("moduleUrl", moduleUrl);
		hashMap.put("userId", userId);
		List<String> permissonList = sysDao.getPermissionFromModuleAndUser(hashMap);
		List<String> deptIDStrings = new ArrayList<String>();
		for (String perString : permissonList) {
			String[] temList = perString.split(",");
			deptIDStrings.addAll(Arrays.asList(temList));
		}
		return deptIDStrings;
	}
}
